package com.cby.android.sample.model

import android.app.Application
import android.text.TextUtils
import com.cby.android.library.base.BaseStateViewModel
import com.cby.android.sample.bean.roll.RollDictionaryBean
import com.cby.android.sample.bean.roll.RollHistoryBean
import com.cby.android.sample.bean.roll.RollPhoneBean
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.repository.RollApiImpl
import com.cby.android.sample.state.RollMoreState

/**
 * @description: RollPhoneAddressViewModel
 *
 * @author ChenBaoYang
 * @date 4/23/21 14:33
 */
class RollMoreViewModel(application: Application) :
    BaseStateViewModel<RollMoreState, RollMoreIntent>(application) {

    private val rollApi = RollApiImpl()


    private fun getMobileLocation(mobile: String) {
        fetchDataWithStatus {
            val response = rollApi.getMobileLocation(mobile)
            updateState(RollMoreState(dataPhone = response.data))
            response.data != null
        }
    }


    private fun getIpLocation(ip: String) {
        fetchDataWithStatus {
            val response = if (TextUtils.isEmpty(ip)) {
                rollApi.getIpLocationSelf()
            } else {
                rollApi.getIpLocation(ip)
            }
            updateState(RollMoreState(dataIp = response.data))
            response.data != null
        }
    }

    private fun getWorldPhoneCode() {
        fetchDataWithStatus {
            val response = rollApi.getWorldPhoneCode()
            val list: MutableList<RollPhoneBean> = mutableListOf()
            response.data?.let { list.addAll(it) }
            updateState(RollMoreState(dataPhoneCodes = list))
            response.data != null
        }
    }

    private fun getHistoryToday() {
        fetchDataWithStatus {
            val response = rollApi.getHistoryToday()
            val list: MutableList<RollHistoryBean> = mutableListOf()
            response.data?.let { list.addAll(it) }
            updateState(RollMoreState(dataHistoryToday = list))
            !list.isEmpty()
        }
    }

    private fun getRubbishType(name: String) {
        fetchDataWithStatus {
            val response = rollApi.getRubbishType(name)
            updateState(RollMoreState(dataRubbish = response.data))
            response.data != null
        }
    }

    private fun convertDictionary(word: String) {
        fetchDataWithStatus {
            val response = rollApi.convertDictionary(word)
            val list: MutableList<RollDictionaryBean> = mutableListOf()
            response.data?.let { list.addAll(it) }
            updateState(RollMoreState(dataDictionary = list))
            response.data != null
        }
    }

    private fun getQRCode(content: String) {
        fetchDataWithStatus {
            val response = rollApi.getQRCode(content)
            updateState(RollMoreState(dataQRCode = response.data))
            response.data != null
        }
    }

    override fun dispatch(intent: RollMoreIntent) {
        when (intent) {
            is RollMoreIntent.LoadRollMobileIntent -> getMobileLocation(intent.mobile)
            is RollMoreIntent.LoadRollIpIntent -> getIpLocation(intent.ip)
            is RollMoreIntent.LoadRollPhoneIntent -> getWorldPhoneCode()
            is RollMoreIntent.LoadRollHistoryIntent -> getHistoryToday()
            is RollMoreIntent.LoadRollRubbishIntent -> getRubbishType(intent.name)
            is RollMoreIntent.LoadRollDictionaryIntent -> convertDictionary(intent.word)
            is RollMoreIntent.LoadRollQRCodeIntent -> getQRCode(intent.content)
        }
    }

}